import time
from datetime import datetime

from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
# from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class CommonDriver:
    def get_driver(self, udid, port):
        options = UiAutomator2Options()
        options.platform_name = 'Android'
        options.platform_version = '7'
        options.app_package = 'com.dangdang.buy2'
        options.app_activity = 'com.dangdang.buy2.StartupActivity'
        options.no_reset = False
        options.unicode_keyboard = True
        options.udid = udid  # 动态传入设备ID

        command_executor_url = f"http://127.0.0.1:{port}/wd/hub"

        driver = webdriver.Remote(command_executor=command_executor_url, options=options)
        driver.implicitly_wait(10)
        print(f"设备 {udid} 连接成功，Appium端口 {port}")
        return driver


if __name__ == '__main__':
    driver = CommonDriver().get_driver()
    driver.find_element(AppiumBy.XPATH,
                        '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    current_time = datetime.now()
    print("当前时间：", current_time)
    try:
        element = WebDriverWait(driver, 20).until(
            EC.visibility_of_element_located(
                (AppiumBy.XPATH, '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sur"]')
            ))
        element.click()
    except BaseException as e:
        print(e)
    finally:
        current_time = datetime.now()
        print("当前时间：", current_time)
        # driver.find_element(AppiumBy.XPATH,
        #                     '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
        # driver.find_element(AppiumBy.XPATH,
        #                     '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/dialog_cancel_tv"]').click()
        # driver.find_element(AppiumBy.XPATH,
        #                     '//android.widget.ViewFlipper[@resource-id="com.dangdang.buy2:id/home_view_flipper"]').click()
        # driver.find_element(AppiumBy.XPATH,
        #                     '//android.widget.EditText[@resource-id="com.dangdang.buy2:id/et_search"]').clear()
        # driver.find_element(AppiumBy.XPATH,
        #                     '//android.widget.EditText[@resource-id="com.dangdang.buy2:id/et_search"]').send_keys(
        #     "浪潮之巅")
        # driver.find_element(AppiumBy.XPATH,
        #                     '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_search"]').click()
        # driver.quit()
        # try:
        #     # 获取屏幕的尺寸
        #     screen_size = driver.get_window_size()
        #     # 获取屏幕的宽度
        #     width = screen_size["width"]
        #     print("-" * 10, width)
        #     # 获取屏幕的高度
        #     height = screen_size["height"]
        #     print("-" * 10, height)
        #
        #     # 定义滑动的起始和结束的坐标
        #     start_x = width / 2  # 从屏幕中间滑动
        #     start_y = height * 0.8  # 从屏幕80%的地方开始滑动
        #     end_x = width / 2
        #     end_y = height * 0.2
        #     print(start_x, start_y, end_x, end_y)
        #
        #     # 开始执行滑动操作
        #     driver.swipe(start_x, start_y, end_x, end_y)
        #     time.sleep(2)
        # except Exception as e:
        #     print(str(e))
        # finally:
        #     driver.quit()
        # driver.tap([(76, 386)], 1000)
        # dangdang = driver.find_element(By.XPATH, '//android.widget.TextView[@text="当当"]')
        #
        # # 创建TouchAction对象
        # driver_action = TouchAction(driver)
        #
        # # 对该元素长按，移动到指定的坐标
        # driver_action.long_press(dangdang).move_to(x=105,y=613).release().perform()
